using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SharpCore.Data;
using SharpCore.Utilities;

namespace Dating.DA
{
	public class MemberAudioDA
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors
		public MemberAudioDA()
		{
		}

		public MemberAudioDA(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the MemberAudio table.
		/// </summary>
		public  int Insert(MemberAudio memberAudio)
		{
			ValidationUtility.ValidateArgument("memberAudio", memberAudio);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", memberAudio.TenantID),
				new SqlParameter("@ProfileID", memberAudio.ProfileID),
				new SqlParameter("@Title", memberAudio.Title),
				new SqlParameter("@SourceFile", memberAudio.SourceFile),
				new SqlParameter("@AudioURL", memberAudio.AudioURL),
				new SqlParameter("@IsApproved", memberAudio.IsApproved),
				new SqlParameter("@Language", memberAudio.Language),
				new SqlParameter("@AddedOn", memberAudio.AddedOn)
			};

			memberAudio.AudioID = Convert.ToInt32(SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "MemberAudioInsert", parameters));
			return memberAudio.AudioID;
		}

		/// <summary>
		/// Updates a record in the MemberAudio table.
		/// </summary>
		public  int Update(MemberAudio memberAudio)
		{
			ValidationUtility.ValidateArgument("memberAudio", memberAudio);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@AudioID", memberAudio.AudioID),
				new SqlParameter("@TenantID", memberAudio.TenantID),
				new SqlParameter("@ProfileID", memberAudio.ProfileID),
				new SqlParameter("@Title", memberAudio.Title),
				new SqlParameter("@SourceFile", memberAudio.SourceFile),
				new SqlParameter("@AudioURL", memberAudio.AudioURL),
				new SqlParameter("@IsApproved", memberAudio.IsApproved),
				new SqlParameter("@Language", memberAudio.Language),
				new SqlParameter("@AddedOn", memberAudio.AddedOn)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MemberAudioUpdate", parameters));
		}

		/// <summary>
		/// Deletes a record from the MemberAudio table by its primary key.
		/// </summary>
		public  int Delete(int audioID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@AudioID", audioID)
,				new SqlParameter("@TenantID", tenantID)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MemberAudioDelete", parameters));
		}

		/// <summary>
		/// Deletes a record from the MemberAudio table by a foreign key.
		/// </summary>
		public  int DeleteAllByProfileID(int profileID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ProfileID", profileID)
,				new SqlParameter("@TenantID", tenantID)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MemberAudioDeleteAllByProfileID", parameters));
		}

		/// <summary>
		/// Deletes a record from the MemberAudio table by a foreign key.
		/// </summary>
		public  int DeleteAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MemberAudioDeleteAllByTenantID", parameters));
		}

		/// <summary>
		/// Selects a single record from the MemberAudio table.
		/// </summary>
		public  MemberAudio Select(int audioID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@AudioID", audioID)
,				new SqlParameter("@TenantID", tenantID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MemberAudioSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MakeMemberAudio(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the MemberAudio table.
		/// </summary>
		public  List<MemberAudio> SelectAll(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)
			};
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MemberAudioSelectAll", parameters))
			{
				List<MemberAudio> memberAudioList = new List<MemberAudio>();
				while (dataReader.Read())
				{
					MemberAudio memberAudio = MakeMemberAudio(dataReader);
					memberAudioList.Add(memberAudio);
				}

				return memberAudioList;
			}
		}

		/// <summary>
		/// Selects all records from the MemberAudio table by a foreign key.
		/// </summary>
		public  List<MemberAudio> SelectAllByProfileID(int profileID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ProfileID", profileID)
,				new SqlParameter("@TenantID", tenantID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MemberAudioSelectAllByProfileID", parameters))
			{
				List<MemberAudio> memberAudioList = new List<MemberAudio>();
				while (dataReader.Read())
				{
					MemberAudio memberAudio = MakeMemberAudio(dataReader);
					memberAudioList.Add(memberAudio);
				}

				return memberAudioList;
			}
		}

		/// <summary>
		/// Selects all records from the MemberAudio table by a foreign key.
		/// </summary>
		public  List<MemberAudio> SelectAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MemberAudioSelectAllByTenantID", parameters))
			{
				List<MemberAudio> memberAudioList = new List<MemberAudio>();
				while (dataReader.Read())
				{
					MemberAudio memberAudio = MakeMemberAudio(dataReader);
					memberAudioList.Add(memberAudio);
				}

				return memberAudioList;
			}
		}

		/// <summary>
		/// Creates a new instance of the MemberAudio class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected  MemberAudio MakeMemberAudio(SqlDataReader dataReader)
		{
			MemberAudio memberAudio = new MemberAudio();
			memberAudio.AudioID = SqlClientUtility.GetInt32(dataReader, "AudioID", 0);
			memberAudio.TenantID = SqlClientUtility.GetInt32(dataReader, "TenantID", 0);
			memberAudio.ProfileID = SqlClientUtility.GetInt32(dataReader, "ProfileID", 0);
			memberAudio.Title = SqlClientUtility.GetString(dataReader, "Title", String.Empty);
			memberAudio.SourceFile = SqlClientUtility.GetString(dataReader, "SourceFile", String.Empty);
			memberAudio.AudioURL = SqlClientUtility.GetString(dataReader, "AudioURL", String.Empty);
			memberAudio.IsApproved = SqlClientUtility.GetString(dataReader, "IsApproved", String.Empty);
			memberAudio.Language = SqlClientUtility.GetString(dataReader, "Language", String.Empty);
			memberAudio.AddedOn = SqlClientUtility.GetDateTime(dataReader, "AddedOn", DateTime.Now);

			return memberAudio;
		}

		#endregion
	}
}
